package org.wattdepot.cli;

import org.wattdepot.cli.commands.UserCommands;
import org.wattdepot.client.WattDepotClient;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.source.jaxb.SourceIndex;
import org.wattdepot.resource.source.jaxb.SourceRef;
import org.wattdepot.util.tstamp.Tstamp;
import java.util.List;
import javax.xml.datatype.XMLGregorianCalendar;

/**
 * A simple command line interface to WattDepot that shows basic connection and processing.
 * 
 * @author Philip Johnson
 */
public class CommandLineInterface {
  /** The URL to the public WattDepot Server. */
  private String hostUri = "http://server.wattdepot.org:8182/wattdepot/";
  /** Holds an instance of the WattDepot client. */
  private WattDepotClient client;
  /** Carriage return on this operating system. */
  private String cr = System.getProperty("line.separator");

  /**
   * Connect to the service and execute some simple commands. Returns immediately if a connection to
   * the service could not be obtained.
   */
  public CommandLineInterface() {
    client = new WattDepotClient(this.hostUri);
    // Stop right away if we cannot contact server.
    if (!client.isHealthy()) {
      System.out.println("Failed to connect to " + client.getWattDepotUri());
      return;
    }
    // Indicate successful connection.
    System.out.println("Connected to " + client.getWattDepotUri());
  }

  /**
   * Returns a string containing a list of all public sources available on this server.
   * 
   * @return A list of sources as a string.
   * @throws WattDepotClientException If problems occur obtaining the list of sources.
   */
  public String getSourcesInfo() throws WattDepotClientException {
    StringBuffer buff = new StringBuffer(20);
    buff.append("All public sources:").append(cr);
    List<Source> sources = client.getSources();
    for (Source source : sources) {
      buff.append(source.getName()).append(cr);
    }
    return buff.toString();
  }

  /**
   * Returns a double indicating the current power generated by a sample source on November 1, 2009.
   * 
   * @param source The source we want the current power from.
   * @return A double indicating power generated.
   * @throws WattDepotClientException If problems occur obtaining the power value.
   */
  public double getPowerGenerated(Source source) throws WattDepotClientException {
    XMLGregorianCalendar now = null;
    try {
      now = Tstamp.makeTimestamp("2009-11-01");
    }
    catch (Exception e) {
      System.out.println("should never happen.");
    }
    return client.getPowerGenerated(source.getName(), now);
  }

  /**
   * Returns a sample Source. It's the first one returned by the WattDepot server in its index of
   * sources.
   * 
   * @return The sample Source.
   * @throws WattDepotClientException If problems occur getting the source index.
   */
  public Source getSampleSource() throws WattDepotClientException {
    SourceIndex index = client.getSourceIndex();
    SourceRef ref = index.getSourceRef().get(0);
    return client.getSource(ref);
  }

  /**
   * Returns a list of all the sources.
   * 
   * @return The sources
   * @throws WattDepotClientException If problems occur getting the source index.
   */
  public List<Source> getSourcesList() throws WattDepotClientException {
    return this.client.getSources();
  }

  /**
   * Returns this class' client.
   * 
   * @return This class' client
   */
  public WattDepotClient getClient() {
    return this.client;
  }

  /**
   * The main program that is invoked from 'java -jar wattdepot-cli.jar'.
   * 
   * @param args All args are ignored.
   * @throws WattDepotClientException If anything goes wrong.
   */
  public static void main(String[] args) throws WattDepotClientException {
    System.out.println("Starting CLI.");
    CommandLineInterface cli = new CommandLineInterface();

    UserCommands userCommands = new UserCommands(cli);
    userCommands.startPrompt();
  }
}
